package com.css.dzj.dzjap.modules.administration.check.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.administration.check.entity.ApJgxwCheckAction;
import com.css.dzj.dzjap.modules.administration.check.service.CheckService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping(value = "/check")
public class CheckController {

    @Autowired
    private CheckService checkService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private  HttpServletRequest request;
    /**
     * 添加(行政检查行为）监管信息信用表
     */
    @PostMapping(value = "/addCheck")
    public R addCheck(@RequestBody ApJgxwCheckAction apJgxwCheckAction){
        String s;
        try{
            String msg = checkService.addCheck(apJgxwCheckAction);
            s = JSON.toJSONString(apJgxwCheckAction);
            sysAuditService.saveSuccessAudit("行政检查行为", Constant.CHECK,Constant.PT,Constant.ADD,s,request);
            return R.ok().put("data", msg);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(apJgxwCheckAction);
            sysAuditService.saveFailAudit("行政检查行为",Constant.CHECK,Constant.PT,Constant.ADD,s,e,request);
            return  R.error("操作失败!");
        }

    }
    /**
     * 通过ID删除(行政检查行为）监管信息信用表
     */
    @PostMapping(value = "/delCheck")
    public R delCheck(@RequestParam("actionId") String actionId){
        String s;
        if(checkService.delCheck(actionId)){
            s = JSON.toJSONString(actionId);
            sysAuditService.saveSuccessAudit("行政检查行为", Constant.CHECK,Constant.PT,Constant.DEL,s,request);
            return R.ok("操作成功!");
        }else{
            s = JSON.toJSONString(actionId);
            RuntimeException e = new RuntimeException("操作失败");
            sysAuditService.saveFailAudit("行政检查行为",Constant.CHECK,Constant.PT,Constant.DEL,s,e,request);
            return  R.error("操作失败!");
        }
    }
    /**
     * 修改（行政检查行为）监管信息信用表
     */
    @PostMapping(value = "/upCheck")
    public R updateCheck(@RequestBody ApJgxwCheckAction apJgxwCheckAction){
        String s;
        try{
            ApJgxwCheckAction check = checkService.getCheck(apJgxwCheckAction.getActionId());
            BeanUtil.copyBeanNotNullToBean(apJgxwCheckAction, check);
            checkService.updateCheck(check);
            s = JSON.toJSONString(check);
            sysAuditService.saveSuccessAudit("行政检查行为", Constant.CHECK,Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(apJgxwCheckAction);
            sysAuditService.saveFailAudit("行政检查行为",Constant.CHECK,Constant.PT,Constant.UPDATE,s,e,request);
            return  R.error("操作失败!");
        }
    }
    /**
     * 通过ID查询（行政检查行为）监管信息信用表
     */
    @GetMapping(value = "/getCheck")
    public R getCheck(@RequestParam("actionId") String actionId){
        String s;
        try {
            ApJgxwCheckAction check = checkService.getCheck(actionId);
            s = JSON.toJSONString(actionId);
            sysAuditService.saveSuccessAudit("行政检查行为", Constant.CHECK,Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", check);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(actionId);
            sysAuditService.saveFailAudit("行政检查行为",Constant.CHECK,Constant.PT,Constant.SELETE,s,e,request);
            return  R.error("操作失败!");
        }
    }
    /**
     *分页查询所有其他行为信息表
     */
    @GetMapping(value = "/getAllCheck")
    public R getAllCheck(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize,HttpServletRequest request ) {
        String s="";
       try{
           PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
           QueryResults results = checkService.getAllCheck(pageRequest);
           HttpSession session= request.getSession();
           s = JSON.toJSONString(results);
           sysAuditService.saveSuccessAudit("行政检查行为", Constant.CHECK,Constant.PT,Constant.PAGE,s,request);
           return R.ok().put("data", results);
       }catch (Exception e){
           e.printStackTrace();
           sysAuditService.saveFailAudit("行政检查行为",Constant.CHECK,Constant.PT,Constant.PAGE,s,e,request);
           return  R.error("操作失败!");
       }


    }
    /**
     * 多条件查询所有其他行为信息表
     */
    @GetMapping(value ="/getAllContation")
    public R getAllContation(@RequestParam String superviseItemImplementCode,
                             @RequestParam(required = false) String checkActionName,
                             @RequestParam(required = false) String checkActionCode,
                             @RequestParam(required = false) String checkDate,
                             @RequestParam(required = false) Integer pageNo,
                             @RequestParam(required = false) Integer pageSize){
        String s="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults results = checkService.getAllContation(pageRequest,superviseItemImplementCode,checkActionName,checkActionCode,checkDate);
            s = JSON.toJSONString(results);
            sysAuditService.saveSuccessAudit("行政检查行为", Constant.CHECK,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data",results );
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政检查行为",Constant.CHECK,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");

        }

    }

      /***
     * 获取检查行为
     * @return
     */
    @GetMapping("/count")
    public R countHavior(){
        HashMap count=checkService.getCount();
        return R.ok().put("count",count);
    }
    /***
     * 获取检查行为根据年度
     * @return
     */
    @GetMapping("/countbyyear")
    public R countHaviorByYear(String year){
        HashMap count=checkService.getCountByYear(year);
        return R.ok().put("count",count);
    }

    /***
     * 获取当前登陆人的机构的检查行为
     * @param request
     * @return
     */
    //@GetMapping("/getcheck")
    public R getOpCheck(HttpServletRequest request){
        Long count=checkService.getOpCheck(request);
        return R.ok().put("count",count);
    }

    /**
     * 风险预警调用接口，通过行为编号查询
     * @param code
     * @return
     */
    @GetMapping("/getAction")
    public R getAction(@RequestParam String code){
        ApJgxwCheckAction apJgxwCheckAction = checkService.getAction(code);
        return R.ok().put("data",apJgxwCheckAction);
    }

    /**
     * 多条件查询所有其他行为信息表
     */
    @GetMapping(value ="/getAll")
    public QueryResults<ApJgxwCheckAction> getAll(@RequestParam String superviseItemImplementCode,
                             @RequestParam(required = false) String checkActionName,
                             @RequestParam(required = false) String checkResult,
                             @RequestParam(required = false) String checkForm,
                             @RequestParam(required = false) String checkType,
                             @RequestParam(required = false) String checkMode,
                             @RequestParam(required = false) String checkDate,
                             @RequestParam(required = false) Integer pageNo,
                             @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        QueryResults<ApJgxwCheckAction> results = checkService.getAll(pageRequest,superviseItemImplementCode,checkActionName,checkResult,checkForm,checkType,checkMode,checkDate);
        return results;
    }

    /***
     * 获取检查行为的总数
     * @return
     */
    @GetMapping("/num")
    public Long getCount(){
      Long num=checkService.getNum();
      return num;
    }

    /***
     * 获取list
     * @param superviseItemImplementCode
     * @param checkActionName
     * @param checkForm
     * @param checkType
     * @param checkMode
     * @param checkDate
     * @return
     */
    @GetMapping(value ="/getList")
    public List<ApJgxwCheckAction> getList(@RequestParam String superviseItemImplementCode,
                    @RequestParam(required = false) String checkActionName,
                    @RequestParam(required = false) String checkResult,
                    @RequestParam(required = false) String checkForm,
                    @RequestParam(required = false) String checkType,
                    @RequestParam(required = false) String checkMode,
                    @RequestParam(required = false) String checkDate){
        List<ApJgxwCheckAction> list = checkService.getList(superviseItemImplementCode,checkActionName,checkResult,checkForm,checkType,checkMode,checkDate);
        return list;
    }

    /***
     * 通过Ids的对外接口
     * @param ids
     * @return
     */
    @GetMapping("/getByIds")
    public List<ApJgxwCheckAction> getByIds(@RequestParam String ids){
        List<String> arr= Arrays.asList(ids.split(","));
        List<ApJgxwCheckAction> list=checkService.getByIds(arr);
        return list;
    }

    /**
     * 通过ID查询（行政检查行为）监管信息信用表对外接口
     */
    @GetMapping(value = "/getCheckById")
    public R getCheckById(@RequestParam("actionId") String actionId){
        ApJgxwCheckAction check = checkService.getCheck(actionId);
        return R.ok().put("data", check);
    }

    /**
     * 查询行政检查行为所有信息
     * 对外提供接口
    *@Author weitao
    *@Date 14:22 2020/6/28
    */
    @GetMapping(value ="/getApList")
    public List<ApJgxwCheckAction> getApList(){

        try {
            List<ApJgxwCheckAction> list = checkService.getApList();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
